/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.titama.control;


import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 *
 * Controller für das Template Task.fxml
 * 
 * @author Thomas Müller
 * 
 */

public class TaskController implements Initializable {
    
    DBConnect dbcon = new DBConnect();
    
    @FXML public TextField TaskName;
    @FXML public ChoiceBox Category;
    @FXML public ChoiceBox Priority;
    @FXML public DatePicker DueDate;
    @FXML public TextArea Notes;
    @FXML private Button SaveButton;
    @FXML public RootController rc ;
    
    ObservableList<String> ChoiceData = FXCollections.observableArrayList();

    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
        for(String s : dbcon.getCategorys())
        {
            ChoiceData.add(s);       
        }
        
        Category.setItems(ChoiceData);
        Category.setValue(ChoiceData.get(1));
        
        Priority.setItems(FXCollections.observableArrayList("Niedrig","Normal","Hoch"));
        Priority.setValue("Normal");


        if(TaskName.getText().equals(""))
        {
            SaveButton.setDisable(true);        
        }  
        

    }  
    
    
    @FXML
    public void Close(ActionEvent e)
    {
        Refresh();
        Node  source = (Node)  e.getSource(); 
        Stage stage  = (Stage) source.getScene().getWindow();
        stage.close();       
    }
    
    
    @FXML
    public void Save(ActionEvent e)
    {
        if(!TaskName.getText().equals(""))
        {
            try
            {
                dbcon.addTask(TaskName.getText(), Priority.getSelectionModel().getSelectedItem().toString(), new SimpleDateFormat("dd.MM.yyyy").format(new Date()), new SimpleDateFormat("dd.MM.yyyy").format(Date.from(DueDate.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant())), Category.getSelectionModel().getSelectedItem().toString(), Notes.getText()); 
                Close(e);
            }
            catch(Exception exe)
            {
              
            }
        }
        else
        {
            TaskName.setPromptText("Bitte geben Sie einen Betreff ein ..");        
        }
    }
    
    @FXML
    public void CheckInput(ActionEvent e)
    {        
        if(DueDate.getValue() != null)
        {
            
            SaveButton.setDisable(false);
        }
        else
        {
            SaveButton.setDisable(true);
        }               
    }
    
    public void SetData(String name, String cat, String prio, String date, String notes)
    {
        TaskName.setText(name);
        Category.setValue(cat);
        Priority.setValue(prio);
        Notes.setText(notes);  
        String datestring =  date.substring(6, 10) + "-" +date.substring(3, 5) +"-" +date.substring(0, 2);
        DueDate.setValue(LocalDate.parse(datestring));

    }
    
    public void Refresh()
    {
        
        try
        {           
            FXMLLoader fxmlLoader = new FXMLLoader();
            URL location = getClass().getResource("/de/titama/view/Main.fxml");                        
            fxmlLoader.setLocation(location);
            fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

            Parent root = (Parent) fxmlLoader.load(location.openStream());
            
            rc = fxmlLoader.getController();
            
            System.out.println(rc.RefreshList());
            rc.RefreshList();
            
            

        }
        catch(Exception exe)
        {
            exe.printStackTrace();
        }
    
    }
  
}
